package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Menu;
import com.yqg.education.service.IMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * MenuController 前端控制器
 * @author zxf
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @ApiOperation("新增或更新菜单")
    public Result save(@RequestBody Menu menu) {
        return Result.success(menuService.saveOrUpdate(menu));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @ApiOperation("根据ID删除菜单")
    public Result delete(@PathVariable Integer id) {
        Menu menu = menuService.getById(id);
        return Result.success(menuService.saveOrUpdate(menu));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    @ApiOperation("查询所有菜单")
    public Result buildMenuTree() {
        List<Menu> list = menuService.list();
        return Result.success(menuService.getTreeMenu(list));
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜单")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜单")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

